﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using Invoicing.Model.Results;
using Invoicing.Model.Products;
using Invoicing.Logic.Products;
namespace Invoicing.Web.Management.Areas.Products.Controllers
{
    [RouteArea("Products")]
    [RoutePrefix("Stock")]
    [Authorize]
    public class ProductStockApiController : ApiController
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<ProductStockModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                ProductStockQuery sea = new ProductStockQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.GetProduct = request.Value<bool>("getProduct");
                sea.ProductID = request.Value<int>("productID");
                sea.MaterialID = request.Value<int>("materialID");
                sea.ProcessID = request.Value<int>("processID");
                sea.Size = request.Value<string>("size");
                sea.HouseID = request.Value<int>("houseID");
                sea.OrderBy = new Model.QueryBase.Sort() { 
                    Field=request.Value<string>("sortField"),
                    Dir=request.Value<string>("sortDir")
                };
                var result = ProductStockLogic.Gets(sea);
                return new JsonEntitySet<ProductStockModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<ProductStockModel>(null, errinfo, false);
        }

        [Route("GetSum")]
        [HttpPost]
        public JsonResult<decimal[]> GetSum(JObject request)
        {
            string errinfo = "no data";
            try
            {
                ProductStockQuery sea = new ProductStockQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.GetProduct = request.Value<bool>("getProduct");
                sea.ProductID = request.Value<int>("productID");
                sea.MaterialID = request.Value<int>("materialID");
                sea.ProcessID = request.Value<int>("processID");
                sea.Size = request.Value<string>("size");
                sea.HouseID = request.Value<int>("houseID");
                
                var result = ProductStockLogic.GetSum(sea);
                return new JsonResult<decimal[]>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<decimal[]>(null, errinfo, false);
        }
    }
}
